// pages/wjlist/index.js
import {
  HTTP
} from '../../utils/http.js'
let Myhttp = new HTTP()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: ['最新问卷','最热问卷', '综合问卷', ],
    questionnaire: [],
    currentTabsIndex:0,
    page:1
  },
  tabsClick: function (e) {
    //console.log(e)
    this.setData({
      currentTabsIndex: e.currentTarget.dataset.index
    })
    let that = this
    let index = that.data.currentTabsIndex + 1
    Myhttp.request({
      url: '/questionnaire/index/index',
      method: 'POST',
      data: {
        page: this.data.page,
        type: index
      },
      success: (res) => {
        console.log(res)
        let datas = res.data.data.list
        for (let i = 0; i < datas.length; i++) {
          datas[i].index = 1
        }
        that.setData({
          'questionnaire': datas
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let index = that.data.currentTabsIndex + 1
    Myhttp.request({
      url: '/questionnaire/index/index',
      method: 'POST',
      data: {
        page: this.data.page,
        type: index
      },
      success: (res) => {
        //console.log(res)
        let datas = res.data.data.list
        for (let i = 0; i < datas.length; i++) {
          datas[i].index = 1
        }
        that.setData({
          'questionnaire': datas
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      questionnaire: [],
      currentTabsIndex: 0,
      page: 1
    })
    let that = this
    let index = that.data.currentTabsIndex + 1
    Myhttp.request({
      url: '/questionnaire/index/index',
      method: 'POST',
      data: {
        page: this.data.page,
        type: index
      },
      success: (res) => {
        //console.log(res)
        let datas = res.data.data.list
        for (let i = 0; i < datas.length; i++) {
          datas[i].index = 1
        }
        that.setData({
          'questionnaire': datas
        })
        wx.showToast({
          title: '刷新成功'
        })
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page
    page++

    this.setData({
      page: page
    })

    let that = this
    let index = that.data.currentTabsIndex + 1
    Myhttp.request({
      url: '/questionnaire/index/index',
      method: 'POST',
      data: {
        page: this.data.page,
        type: index
      },
      success: (res) => {
        //console.log(res)
        let datas = res.data.data.list
        let data = this.data.questionnaire
        for (let i = 0; i < datas.length; i++) {
          datas[i].index = 1
        }
        datas.forEach(val=>{
          data.push(datas)
        })
        that.setData({
          'questionnaire': data
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/questionnaireList/index'
    }
  }
})